package com.wfy.common.config;

import com.google.common.collect.Maps;
import com.wfy.common.util.PropertiesLoader;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: wangxindong
 * Date: 2017/7/1
 * Time: 22:12
 */
public final class Global {

    private Global() {
    }

    public static Global getInstance() {
        return LazyHolder.GLOBAL;
    }

    private static class LazyHolder {
        private static final Global GLOBAL = new Global();
    }

    /**
     * https认证请求头
     */
    public static final String AUTHORIZATION = "Authorization";

    public static final String XML_HEADER = "X-Requested-With";

    public static final String User_Agent = "User-Agent";

    /**
     * 登录接口
     */
    public static final String LOGIN_URL = "login";

    /**
     * 保存全局属性值
     */
    private static Map<String, String> map = Maps.newConcurrentMap();

    public static final String DEFAULT_CHARTSET = "UTF-8";


    /**
     * 加密
     */
    public static final String HASH_ALGORITHM = "SHA-1";
    public static final int HASH_INTERATIONS = 1024;
    public static final int SALT_SIZE = 8;


    /**
     * 属性文件加载对象
     */
    private static PropertiesLoader loader = new PropertiesLoader("application.properties");

    /**
     * 获取配置
     */
    public static String getConfig(String key) {
        String value = map.get(key);
        if (value == null) {
            value = loader.getProperty(key);
            map.put(key, value != null ? value : StringUtils.EMPTY);
        }
        return value;
    }


    public static final String TOKEN = "token";

    public static final String COOKIE_NAME = "SHIROCOOKIE";

    /**
     * 默认分隔符
     */
    public static final String DEFAULT_SPLIT = "_";

    public static final String DEFAULT_POINT = ".";

    /**
     * 参数分隔符
     */
    public static final String DEFAULT_PARAM_SPLIT = "-";

    public static final String YES = "true";
    public static final String NO = "false";

    /**
     * 显示/隐藏
     */
    public static final String SHOW = "1";
    public static final String HIDE = "0";


    public static final String DEFAULT_SUCCESS_MSG = "SUCCESS";
    public static final int DEFAULT_SUCCESS_CODE = 0;

    public static final String DEFAULT_FAIL_MSG = "FAIL";
    public static final int DEFAULT_FAIL_CODE = 1;

    /**
     * 数据验证错误
     */
    public static final String DEFAULT_VALIDATOR_MSG = "VALIDATOR_ERROR";
    public static final int DEFAULT_VALIDATOR_CODE = 13;

    /**
     * key不正确
     */
    public static final String INVALID_USER_KEY_MSG = "INVALID_USER_TOKEN";
    public static final int INVALID_USER_KEY_CODE = 2;

    /**
     * 参数非法
     */
    public static final String INVALID_PARAMS_MSG = "INVALID_PARAMS";
    public static final int INVALID_PARAMS_CODE = 3;

    /**
     * 缺失参数
     */
    public static final String MISSING_REQUIRED_PARAMS_MSG = "MISSING_REQUIRED_PARAMS";
    public static final int MISSING_REQUIRED_PARAMS_CODE = 4;

    /**
     * 装置不存在
     */
    public static final String NOT_LOGIN_MSG = "NOT_LOGIN";
    public static final int NOT_LOGIN_CODE = 5;

    /**
     * 超出日访问量
     */
    public static final String DAILY_QUERY_OVER_LIMIT_MSG = "DAILY_QUERY_OVER_LIMIT";
    public static final int DAILY_QUERY_OVER_LIMIT_CODE = 11;

    /**
     * 单位时间内访问过于频繁
     */
    public static final String ACCESS_TOO_FREQUENT_MSG = "ACCESS_TOO_FREQUENT";
    public static final int ACCESS_TOO_FREQUENT_CODE = 12;

    /**
     * 没有使用权限
     */
    public static final String SERVICE_NOT_AVAILABLE_MSG = "SERVICE_NOT_AVAILABLE";
    public static final int SERVICE_NOT_AVAILABLE_CODE = 7;


    /**
     * 用户存在
     */
    public static final String USER_EXIST_MSG = "USER_EXIST";
    public static final int USER_EXIST_CODE = 20;

    /**
     * 默认token键
     */
    public static final String DEFAULT_CACHE_TOKEN_NAMESPACE = "webapi_token";


    /**
     * 获取URL后缀
     */
    public static String getUrlSuffix() {
        return getConfig("urlSuffix");
    }

}
